import axios from 'axios'

// 封装核心方法
const request = (method, url, data, successCallback, errorCallback) => {
    return axios({
        method,    // 动态指定请求方法
        url,
        data,      // POST/PUT 请求体
        params: method.toLowerCase() === 'get' ? data : null // GET 请求参数
    })
        .then(response => {
            if (successCallback) successCallback(response.data)
            return response.data
        })
        .catch(error => {
            if (errorCallback) errorCallback(error)
            return Promise.reject(error)
        })
}

// 快捷方法封装
const get = (url, params, success, error) =>
    request('get', url, params, success, error)

const post = (url, data, success, error) =>
    request('post', url, data, success, error)

export default { get, post }